﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ccit.Utils;
using Ccit.OA.MsPersonDoc;
using Newtonsoft.Json;
using System.Collections.Generic;


namespace Ccit.OA.PersonDoc
{
    public partial class persondoc_mydoc_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initializePage();
                Session["folderId"] = (null != Session["folderId"]) ? Session["folderId"].ToString() : "00";

                this._hidSysDocId.Value = (null != Request.QueryString["SysDocId"]) ? Request.QueryString["SysDocId"].ToString() : "";
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                this._hidNodeTab.Value = (null != Request.QueryString["NodeTab"]) ? Request.QueryString["NodeTab"].ToString() : "0";
            }
        }

        private void initializePage()
        {
            createTree();
            trvperson.ExpandLevel = 1;
          
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string sOrganName = "个人文档";

            oRoot.Text = sOrganName;
            oRoot.ImageUrl = "../images/toolbar/dept_open.gif";
            oRoot.ID = "0_0";
        }


        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvperson.Nodes.Add(oRoot);
            BindTree(oRoot);
        }

        /// <summary>
        /// 绑定树
        /// </summary>
        private void BindTree(Microsoft.Web.UI.WebControls.TreeNode node)
        {
            #region 默认三个节点 我的文档、我的共享文档、他人共享文档

            #endregion

            PersonDocBLL Bll = new PersonDocBLL();
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            MsFolder ms = new MsFolder();
            ms.UserId = loginUser.UserId;
            DataSet dSet = Bll.GetPersonDocfolder(ms,"","");

            DataView dv = new DataView(dSet.Tables[0]);
            AddTreePronNodes(dv, node);//为树添加节点

            Bll.Release();
            Bll = null;
            ms = null;
        }


        /// <summary>
        /// 为树添加节点
        /// </summary>
        /// <param name="dv"></param>
        /// <param name="node"></param>
        private void AddTreeNodes(DataView dv, Microsoft.Web.UI.WebControls.TreeNode node, string sNondtype)
        {
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string sParam = string.Empty;
            string encodeparam = string.Empty;
            string sShare = string.Empty;
            DataView dvSun = new DataView(dv.Table);
            #region 动态添加子节点
            if (sNondtype != "-2")
            {
                foreach (DataRowView dr in dv)
                {
                    sShare = Getshare(dr["DOC_A1_60"].ToString().Trim(), dr["DOC_A1_70"].ToString().Trim(), dr["DOC_A1_80"].ToString().Trim(), dr["DOC_A1_90"].ToString().Trim());
                    Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                    td.Type = "root";
                    td.Text = dr["DOC_A1_20"].ToString();
                    td.ID = dr["DOC_A1_10"].ToString();
                    td.ImageUrl = "../images/toolbar/folder_open.gif";
                    td.Target = "iFrmEdit";

                    #region 子节点添加
                   
                    if (sNondtype == "0")
                    {
                        sParam = "folderID=" + dr["DOC_A1_10"].ToString() + "&folderType=" + sNondtype + "&Share=00";
                        encodeparam = Base64Lib.Encode(sParam);
                        td.NavigateUrl = "persondoc_mydoc_list.aspx?" + encodeparam;
                        node.Nodes.Add(td);
                        dvSun.RowFilter = "DOC_A1_30 = '" + dr["DOC_A1_10"].ToString() + "'";
                        if (0 < dvSun.Count)
                        {
                            AddTreeNodes(dvSun, td, sNondtype);
                        }
                    }
                    else if(sNondtype == "-1")
                    {
                        sParam = "folderID=" + dr["DOC_A1_10"].ToString() + "&folderType=" + sNondtype + "&Share=00";
                        encodeparam = Base64Lib.Encode(sParam);
                        td.NavigateUrl = "persondoc_mydoc_list.aspx?" + encodeparam;
                        node.Nodes.Add(td);
                    }
                    #endregion
                }
            }
            else
            {
                PersonDocBLL Bll = new PersonDocBLL();
                List<string> lstResult = new List<string>();
                string sQuery = " and DOC_A1_50 = '1' and DOC_A1_40 <> " + loginUser.UserId;
                lstResult = Bll.GetOthPersons(sQuery);
                for (int i = 0; i < lstResult.Count; i++)
                {
                    Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                    string[] ResultObj = lstResult[i].Split(',');
                    td.Type = "root";
                    td.Text = ResultObj[1];
                    td.ID = ResultObj[0]+"_user";
                    td.ImageUrl = "../images/toolbar/loginUser.gif";
                    td.Target = "iFrmEdit";
                    node.Nodes.Add(td);
                    dvSun.RowFilter = "DOC_A1_50 = '1' and DOC_A1_40 = '" + ResultObj[0] + "'";
                    if (0 < dvSun.Count)
                    {
                        AddTreeNodesOtherShare(dvSun, td, sNondtype,"");
                    }
                    if (td.Nodes.Count == 0)
                    {
                        td.Remove();
                    }
                }
            }

            #endregion
        }

        private void AddTreeNodesShare(DataView dv, Microsoft.Web.UI.WebControls.TreeNode node, string sNondtype, string sShare,string sUserId)
        { 
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string sParam = string.Empty;
            string encodeparam = string.Empty;
            foreach (DataRowView dr in dv)
            {
                Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                td.Type = "root";
                td.Text = dr["DOC_A1_20"].ToString();
                td.ID = dr["DOC_A1_10"].ToString();
                td.ImageUrl = "../images/toolbar/folder_open.gif";
                td.Target = "iFrmEdit";

                sParam = "folderID=" + dr["DOC_A1_10"].ToString() + "&folderType=" + sNondtype + "&Share=" + sShare + "&UserIdShare=" + sUserId;
                encodeparam = Base64Lib.Encode(sParam);
                td.NavigateUrl = "persondoc_mydoc_list.aspx?" + encodeparam;

                node.Nodes.Add(td);

                #region 添加子节点
                DataView dvSun = new DataView(dv.Table);
                dvSun.RowFilter = " DOC_A1_30 = '" + dr["DOC_A1_10"].ToString() + "' and DOC_A1_40 <> " + loginUser.UserId;
                if (0 < dvSun.Count)
                {
                    AddTreeNodesShare(dvSun, td, sNondtype,sShare,dr["DOC_A1_40"].ToString());
                }
                #endregion
            }
        }

        private void AddTreeNodesOtherShare(DataView dv, Microsoft.Web.UI.WebControls.TreeNode node, string sNondtype, string sShare)
        { 
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string sParam = string.Empty;
            string encodeparam = string.Empty;
       
            foreach (DataRowView dr in dv)
            {
                sShare = Getshare(dr["DOC_A1_60"].ToString().Trim(), dr["DOC_A1_70"].ToString().Trim(), dr["DOC_A1_80"].ToString().Trim(), dr["DOC_A1_90"].ToString().Trim());
                Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                td.Type = "root";
                td.Text = dr["DOC_A1_20"].ToString();
                td.ID = dr["DOC_A1_10"].ToString();
                td.ImageUrl = "../images/toolbar/folder_open.gif";
                td.Target = "iFrmEdit";

                sParam = "folderID=" + dr["DOC_A1_10"].ToString() + "&folderType=" + sNondtype + "&UserIdShare=" + dr["DOC_A1_40"].ToString() + "&Share=" + sShare;
                encodeparam = Base64Lib.Encode(sParam);
                td.NavigateUrl = "persondoc_mydoc_list.aspx?" + encodeparam;

                if (sShare != "05")
                {
                    node.Nodes.Add(td);

                    #region 添加子节点
                    DataView dvSun = new DataView(dv.Table);
                    dvSun.RowFilter = "DOC_A1_30 = " + dr["DOC_A1_10"].ToString() +" and DOC_A1_40 <> " + loginUser.UserId;
                    if (0 < dvSun.Count)
                    {
                        AddTreeNodesShare(dvSun, td, sNondtype, sShare,dr["DOC_A1_40"].ToString());
                    }
                    #endregion
                }
                //sShare = "";
            }
        }
        /// <summary>
        /// 添加第一级父节点
        /// </summary>
        /// <param name="dv">数据集</param>
        /// <param name="node">父节点对象</param>
        private void AddTreePronNodes(DataView dv, Microsoft.Web.UI.WebControls.TreeNode node)
        {
            DataView dvSun = new DataView(dv.Table);
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            string sParam = string.Empty;
            string encodeparam = string.Empty;
            string sNondtype = string.Empty;

            #region
            for (int i = 1; i <= 3; i++)
            {
                Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                sParam = "folderID=" + "0" + "&folderType=" + "0" + "&Share=00";
                encodeparam = Base64Lib.Encode(sParam);
            
                switch (i.ToString())
                {
                    case "1":
                        td.Text = "我的文档";
                        td.ID = sNondtype = "0";
                        td.ImageUrl = "../images/toolbar/mydoc.gif";
                        dvSun.RowFilter = "DOC_A1_30 = '" + "0" + "' and DOC_A1_40 = " + loginUser.UserId;
                        td.NavigateUrl = "persondoc_mydoc_list.aspx?" + encodeparam;
                        td.Target = "iFrmEdit";
                        break;
                    case "2":
                        td.ID = sNondtype = "-1";
                        td.Text = "我的共享文档";
                        td.ImageUrl = "../images/toolbar/sharedoc.gif";
                        dvSun.RowFilter = "DOC_A1_50 = '1' and DOC_A1_40 = " + loginUser.UserId;
                        break;
                    case "3":
                        td.ID = sNondtype = "-2";
                        td.Text = "他人共享";
                        td.ImageUrl = "../images/toolbar/otherdoc.jpg";
                        dvSun.RowFilter = "DOC_A1_50 = '1' and DOC_A1_40 <> " + loginUser.UserId;
                        break;
                    default:
                        break;
                }
                td.Type = "root";
                node.Nodes.Add(td);
                if (0 < dvSun.Count)
                {
                    AddTreeNodes(dvSun, td, sNondtype);
                }
            }
            #endregion
        }

        /// <summary>
        /// 返回文件夹共享标志
        /// </summary>
        /// <param name="sScope">共享范围</param>
        /// <param name="sType">共享类别</param>
        /// <returns>00完全共享 ； 01只读共享；05未共享给当前人</returns>
        private string Getshare(string sReadScope, string sReadType,string sComplScope,string sComplType)
        {
            string sReturn="05";
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            string sUserID = "," + loginUser.UserId + ",";
            //根据共享范围与共享标志得到文件夹 只读的共享用户ID串
            string sReadIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(sReadScope, sReadType, loginUser.OrganId) + ",";
            //根据共享范围与共享标志得到文件夹 完全的共享用户ID串
            string sCompleteIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(sComplScope,sComplType,loginUser.OrganId)+",";
            if (sReadIdList.IndexOf(sUserID) > -1)
            {
                sReturn = "01";
            }
            if (sCompleteIdList.IndexOf(sUserID) > -1)
            {
                sReturn = "00";
            }

            return sReturn;
        }

        //private string GetOthPersons(String sQuery)
        //{
            
        //}
    }
}
